package com.qf.servlet;

import com.qf.Enity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/show")
public class ShowServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*req.setCharacterEncoding("utf-8");*/
        /*resp.setContentType("text/html;charset=utf-8");*/
        PrintWriter out = resp.getWriter( );
        HttpSession session = req.getSession();
        System.out.println("show中的session："+session.getId());
        User user = (User) session.getAttribute("user");
        if (user!=null){
            /*  User user = (User) req.getAttribute("user");*/
            out.write("<html>");
            out.write("<head>");
            out.write("<title>主页</title>");
            out.write("</head>");
            out.write("<body>");
            out.write("<h1>登录成功,欢迎"+user.getUsername()+"</h1>");
            out.write("<button>");
            out.write("<a href='/logout'>退出登录<a>");
            out.write("</button>");
            out.write("</body>");
            out.write("</html>");
        }else {
            resp.sendRedirect("login1.html");

        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doPost(req, resp);
    }
}
